<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]>      <html class="no-js"> <!--<![endif]-->
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"> </script>
  </head>
  <body>

    
    <div id="inputTxtArea" contenteditable="true" style="width:300px;height:300px;border:1px solid black;"></div>
   <script>
   

$(document).keydown(function (e) {
    var e = e || window.event, ec = e.keyCode || e.which;
    // if (!e.ctrlKey && 13 == ec) {
    //     //console.log('发送');
    //     // sendMsgTal();
    //     return false;
    // }
    if (13 == ec) {
        //console.log('换行');
        var ele
        if (browserType() == "IE" || browserType() == "Edge") {
          ele = document.createElement('div')
        }
        else if (browserType() == "FF") {
          ele = document.createElement('br')
        } else {
          ele = document.createElement('div')
        }
        console.log( window.getSelection())
        return
        $("#inputTxtArea").append(ele);
        //设置输入焦点
        // var o = document.getElementById("inputTxtArea").lastChild;            
        var textbox = document.getElementById('inputTxtArea');
        var sel = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(textbox);
        range.collapse(false);
        range.setEndAfter(ele);//
        range.setStartAfter(ele);//
        sel.removeAllRanges();
        sel.addRange(range);
    } else {
      $("#inputTxtArea").append(e.key)
    }
});
function browserType () {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isOpera = false;
        if (userAgent.indexOf('Edge') > -1) {
            return "Edge";
        }
        if (userAgent.indexOf('.NET') > -1) {
            return "IE";
        }
        if (userAgent.indexOf("Opera") > -1 || userAgent.indexOf("OPR") > -1) {
            isOpera = true;
            return "Opera"
        }; //判断是否Opera浏览器
        if (userAgent.indexOf("Firefox") > -1) {
            return "FF";
        } //判断是否Firefox浏览器
        if (userAgent.indexOf("Chrome") > -1) {
            return "Chrome";
        }
        if (userAgent.indexOf("Safari") > -1) {
            return "Safari";
        } //判断是否Safari浏览器
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
            return "IE";
        }; //判断是否IE浏览器
}
   </script>
  </body>
</html>